<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMemberAccountTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
        //用户财务资金表
        Schema::create('member_accounts', function ($table) {
            $table->increments('id');
            $table->integer('member_id')->unsigned()->default(0)->comment('会员ID');
            $table->tinyInteger('account_type')->default(0)->comment('状态：0:积分账户');
            $table->decimal('current_money',12,2)->default('0.00')->comment('当前余额');
            $table->timestamps();
            $table->foreign('member_id')->references('id')->on('members');

            $table->index('member_id');
            $table->index('account_type');

        });

        //用户财务明细表
        Schema::create('member_account_log', function ($table) {
            $table->increments('id');
            $table->integer('member_id')->unsigned()->default(0)->comment('会员ID');
            $table->integer('order_id')->unsigned()->default(0)->comment('订单ID');
            $table->tinyInteger('operate_type')->default(0)->comment('操作类型：0:入账；1:扣款；2:第三方支付；');
            $table->integer('operator_id')->unsigned()->default(0)->comment('操作者UserId');
            $table->tinyInteger('account_type')->default(0)->comment('状态：0:积分账户;1:校园卡账户；2:微信账户；');
            $table->decimal('current_money',12,2)->default('0.00')->comment('当前余额');
            $table->decimal('happen_money',12,2)->default('0.00')->comment('发生金额');
            $table->text('description')->comment('描述');
            $table->timestamps();
            $table->foreign('member_id')->references('id')->on('members');
            $table->foreign('operator_id')->references('id')->on('users');

            $table->index('member_id');
            $table->index('order_id');
            $table->index('operate_type');
            $table->index('operator_id');
            $table->index('account_type');

        });
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
        //
        Schema::table('member_accounts', function(Blueprint $table) {
            $table->dropForeign('member_accounts_member_id_foreign');
        });
        Schema::drop('member_accounts');

        //
        Schema::table('member_account_log', function(Blueprint $table) {
            $table->dropForeign('member_account_log_member_id_foreign');
            $table->dropForeign('member_account_log_operator_id_foreign');
        });
        Schema::drop('member_account_log');
	}

}
